package com.witframework.dynoengine;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.util.WebUtils;

import com.witframework.WitException;
import com.witframework.core.persistence.JdbcTools;
import com.witframework.dynoengine.render.Renderer;
import com.witframework.dynoengine.render.page.DelDataPageRender;
import com.witframework.dynoengine.render.page.ListDataPageRender;
import com.witframework.dynoengine.render.page.UpdateDataPageRender;
import com.witframework.dynoengine.render.page.ViewDataPageRender;
import com.witframework.web.mvc.Controller;
import com.witframework.web.mvc.WebContext;

@Component("dynoServlet")
public class DynoServlet implements Controller{
	public String executor(WebContext webContext){
		
		String dynoPageType=webContext.getParameter("dynoPageType");//页面类型：list/update/view
		if(StringUtils.isEmpty(dynoPageType)) dynoPageType="list";
		String dynoTableId=webContext.getParameter("dynoTableId");//表ID
		String dynoTableName=webContext.getParameter("dynoTableName");//表名称
		String dynoRenderClass=webContext.getParameter("dynoRenderClass");//页面的渲染器类名称
		Renderer renderer=null;
		String renderResult=null;
		if(StringUtils.isNotEmpty(dynoRenderClass)){//使用指定的渲染器
			try {
				renderer= ((Renderer)Class.forName(dynoRenderClass).newInstance());				
			} catch (Exception e) {
				throw new WitException(e);
			}
		}else if(StringUtils.isNotEmpty(dynoPageType)){//使用默认的渲染器
			if(dynoPageType.equalsIgnoreCase("list")){
				renderer=new ListDataPageRender();
			}else if(dynoPageType.equalsIgnoreCase("view")){
				renderer=new ViewDataPageRender();
			}else if(dynoPageType.equalsIgnoreCase("update")){
				renderer=new UpdateDataPageRender();
			}else if(dynoPageType.equalsIgnoreCase("delete")){
				renderer=new DelDataPageRender();
			}else{
				
			}
		}
		if(renderer!=null){
			//设置渲染器参数表
			renderer.setParameter(Renderer.PARAM_httpRequest, webContext.getHttpRequest());
			renderer.setParameter(Renderer.PARAM_httpResponse, webContext.getHttpResponse());
			renderer.setParameter(Renderer.PARAM_tableName, dynoTableName.toUpperCase());
			renderer.setParameter(Renderer.PARAM_tableId, dynoTableId);
			//调用渲染
			renderResult= renderer.render();
		}
		if(renderResult!=null){
			webContext.renderHtml(renderResult);
			return null;
		}
		throw new WitException("未指定页面渲染器或指定了错误的目标类型。\n" +
				"页面类型:"+dynoPageType+";渲染器:"+dynoRenderClass+";数据标识:"+dynoTableName);
	}
	
	public String store(WebContext webContext){
		String action=webContext.getParameter("action");
		String tableName=webContext.getParameter("dynoTableName");//表名称
		Map<String, String> data = WebUtils.getParametersStartingWith(webContext.getHttpRequest(), "");
		
		if(action==null||action.equalsIgnoreCase("save")){
			JdbcTools.insertByWebMap(tableName, data);
		}else if(action.equalsIgnoreCase("update")){
			JdbcTools.updateByWebMap(tableName, data);
		}else  if(action.equalsIgnoreCase("delete")){
			JdbcTools.deleteByWebMap(tableName, data);
		}else{
			throw new WitException("未指定动作类型。" );
		}
		return "redirect:dynoServlet.wit?dynoTableName="+tableName;
	}
	public String delete(WebContext webContext){		
		String tableName=webContext.getParameter("dynoTableName");//表名称
		Map<String, String> data = WebUtils.getParametersStartingWith(webContext.getHttpRequest(), "");		
		JdbcTools.deleteByWebMap(tableName, data);		
		return "redirect:dynoServlet.wit?dynoTableName="+tableName;
	}
}
